﻿//
// GNS_UserTopUp.cpp
// MobileManageSys
//FuncTion 个人充值/兑奖查询
// Created by mr.l on 16/1/14.
//
//
#include "GnsClientFrame.h"
#include "GNS_UserTopUp.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "CSVParser.h"
#include "GNS_PlayerMgr.h"
#include "GNS_HallScene.h"
#include "GNS_SceneMgr.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;
using namespace CSVParser;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;
GNS_UserTopUp *GNS_UserTopUp::pSony = nullptr;
GNS_UserTopUp::GNS_UserTopUp()
{
    pGNS_UserTopUp=0;
    pNewManage=0;
    pManPopWnd=0;
    TimeText=nullptr;
    year=0;
    pUserTopUpDea=nullptr;
    pListView=nullptr;
    ImageeViewType=nullptr;
    TypeText=nullptr;
    pTypeList=nullptr;
    pCalendar=nullptr;
    pMakeLayer= nullptr;
}

GNS_UserTopUp::~GNS_UserTopUp()
{
    
}

GNS_UserTopUp* GNS_UserTopUp::createLayer()
{
    auto pGNS_UserTopUp = GNS_UserTopUp::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    
    pSony =(GNS_UserTopUp*)pGNS_UserTopUp;
    pScene->addChild(pGNS_UserTopUp);
    return pGNS_UserTopUp;
}

bool GNS_UserTopUp::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    ReadCsv();
    return true;
}
void GNS_UserTopUp::ReadCsv()
{
    string path = FileUtils::getInstance()->fullPathForFilename("Manage.csv");
    Csv csv = Csv(path.c_str());
    for (int i =1; i < 6; i++) {
        for (int j = 1; j < csv[i].size(); j++) {
            _DateStr.push_back(csv[i][j]);
        }
    }
}
void GNS_UserTopUp::SetNodeName(std::string CsbName)
{
    pGNS_UserTopUp = CSLoader::createNode(CsbName);
    pGNS_UserTopUp->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_UserTopUp);
    this->addChild(pGNS_UserTopUp,11);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_UserTopUp);
    auto pBackBtn = static_cast<Button*>(pGNS_UserTopUp->getChildByName("UserTopBackBtn"));
    pBackBtn->setTag(10);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
    pNewManage = static_cast<Button*>(pGNS_UserTopUp->getChildByName("TextButton"));
    if (GNS_PlayerMgr::getInstance()->getUserNotInfo()->size()!=0||GNS_PlayerMgr::getInstance()->getUserFailExInfo()->size()!=0) {
        pNewManage ->setVisible(true);
    }else{
        pNewManage ->setVisible(false);
    }
    pNewManage->setTag(1);
    pNewManage->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
    auto TimeButton = static_cast<Button *>(pGNS_UserTopUp->getChildByName("Button_7"));
    TimeButton->setTag(3);
    TimeButton->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
    auto TypeButton = static_cast<Button*>(pGNS_UserTopUp->getChildByName("Button_6"));
    TypeButton->setTag(4);
    TypeButton->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_UserTopUp->getChildByName("Button_8"));
    pTimeSer->setTag(3);
    pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_UserTopUp->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(4);
    typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
    
    pListView = static_cast<ListView *>(pGNS_UserTopUp->getChildByName("ListView_1"));
    
//    pListView->addEventListener(CC_CALLBACK_2(GNS_UserTopUp::SelectedItemEvents, this));
//    pListView->addEventListener(CC_CALLBACK_2(GNS_UserTopUp::selectedItemEventScrollView, this));
//    auto pbutton = static_cast<Button *>(pListView->getChildByName("UserTopUpBtn"));
//    pbutton->setTag(5);
//    pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
    
	time_t tt;
	time(&tt);
	struct tm * time;
	time = localtime(&tt);

	int Mon = (int)time->tm_mon + 1;
	year = time->tm_year + 1900;
	int day = (int)time->tm_mday;

    TimeText=static_cast<Text*>(pGNS_UserTopUp->getChildByName("TimeText"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",year,Mon,day));
    TypeText=static_cast<Text*>(pGNS_UserTopUp->getChildByName("TypeText"));
    ImageeViewType = static_cast<ImageView *>(pGNS_UserTopUp->getChildByName("TypeImage"));
    
    pTypeList = ListView::create();
    pTypeList->setPosition(Vec2(ImageeViewType->getPositionX()-ImageeViewType->getContentSize().width
                                /2-10, ImageeViewType->getPositionY()-ImageeViewType->getContentSize().height/2 -(24*_DateStr.size())));
    pTypeList->setDirection(ui::ScrollView::Direction::VERTICAL);
    pTypeList->setBounceEnabled(true);
    pTypeList->setBackGroundImage("drop.png");
    pTypeList->setBackGroundImageScale9Enabled(true);
    pTypeList->setContentSize(Size(ImageeViewType->getContentSize().width+20, 25*_DateStr.size()));
    pTypeList->setScrollBarPositionFromCorner(Vec2(7, 7));
    pGNS_UserTopUp->addChild(pTypeList,20);
    for (int i =0; i<_DateStr.size(); ++i) {
        auto AllType = Button::create("p0Btn.png");
        AllType->setTitleText(_DateStr.at(i));
        AllType->setTitleColor(Color3B::BLACK);
        AllType->setTitleFontSize(24);
        AllType->setTag(6);
        AllType->setPosition(Vec2(10, 0));
        AllType->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
        pTypeList->addChild(AllType);
    }
    pTypeList->setVisible(false);
    
	/*if (GNS_PlayerMgr::getInstance()->getUserFailExInfo()->size()>0) {
		std::string msgDate=StringUtils::format("%s",GNS_PlayerMgr::getInstance()->getUserFailExInfo()->at(0)._msgContent);
		GetFailDate(msgDate);
		FaillMsgID=GNS_PlayerMgr::getInstance()->getUserFailExInfo()->at(0)._msgId;
		}*/
}

void GNS_UserTopUp::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 10:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                GNS_PlayerMgr::getInstance()->getUserInfo()->clear();
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //SystemNode->setVisible(true);
               // unschedule(schedule_selector(GNS_UserTopUp::RefreshManage));

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->getUserFailExInfo()->size()!=0) 
				{
                    std::string msgDate=StringUtils::format("%s",GNS_PlayerMgr::getInstance()->getUserFailExInfo()->at(0)._msgContent);
                    GetFailDate(msgDate);
                    FaillMsgID=GNS_PlayerMgr::getInstance()->getUserFailExInfo()->at(0)._msgId;
                }
				else if (GNS_PlayerMgr::getInstance()->getUserNotInfo()->size()!=0)
                {
					   std::string text = CCGnsOften::getGB2312_DirectAssignmentInProgram("充值/兑奖信息:");
                        text += StringUtils::format("%s",GNS_PlayerMgr::getInstance()->getUserNotInfo()->at(0)._msgContent);
						text += CCGnsOften::getGB2312_DirectAssignmentInProgram("\n备注信息:\n");
						text += StringUtils::format("%s",GNS_PlayerMgr::getInstance()->getUserNotInfo()->at(0)._remarkMsg);
                        MagID=GNS_PlayerMgr::getInstance()->getUserNotInfo()->at(0)._msgId;
                        pManPopWnd = PopWndLayer::createScene("Popwnd.png", "send_back.png");
                        pManPopWnd->SetTitleWithUTF("充值/兑奖信息");
                        pManPopWnd->SetContentTextWithGB(text, Color3B(0, 0, 0));
						pManPopWnd->getContentText()->setTextHorizontalAlignment(TextHAlignment::LEFT);
                        pManPopWnd->setClickWndOutsideClose(true);
                        auto pButton=pManPopWnd->SetCompleteButton("confirm_Blasting.png", "confirm_Blasting1.png", "confirm_Blasting.png");
                        pButton->setScale(0.5f);
                        pButton->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
                        pButton->setTag(2);
                        pManPopWnd->GetCloseButton()->setPosition(Vec2(pButton->getPositionX()*2.5, pButton->getPositionY()));
                        pManPopWnd->GetCloseButton()->setScale(0.5f);
                        pManPopWnd->GetCloseButton()->setTag(7);
                        pManPopWnd->GetCloseButton()->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
                }
				//RemoveItem();			
                break;
            }
            case 2:
            {
                CMD_CS_SetExchangeInfo pSetInfo;
                pSetInfo._msgId=MagID;
                pSetInfo._isGet=1;//领取
                //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_SET_EXCHANGE_INFO, &pSetInfo, sizeof(pSetInfo));
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_SET_EXCHANGE_INFO, (char*)&pSetInfo, sizeof(pSetInfo));
                pManPopWnd->Show(false);
                vector<CMD_SC_GetUserNotExchangeInfoResult>::iterator it =GNS_PlayerMgr::getInstance()->getUserNotInfo()->begin();
                GNS_PlayerMgr::getInstance()->getUserNotInfo()->erase(it);
                RemoveItem();
                break;
            }
            case 3:
            {
                /*pCalendar = GNS_Calendar::createCalendar();
                pCalendar->SetCalendBgImage("Calendarbg.png");
                std::vector<Button *> CalenderBtn;
                pCalendar->GetTimeButton()->swap(CalenderBtn);
                for (auto & it:CalenderBtn) {
                    it->setTag(100);
                    it->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::TimeButtonCallBack, this));
                }*/
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_UserTopUp::TimeButtonCallBack, this));
                break;
            }
            case  4:
            {
                if (pTypeList->isVisible()==true) {
                    pTypeList->setVisible(false);
                }else{
                    pTypeList->setVisible(true);
                }
                break;
            }
            case 5:
            {
                pGNS_UserTopUp->setVisible(false);
                pUserTopUpDea=GNS_UserTopUpDea::createLayer();
                pUserTopUpDea->SetNodeName("SysTopAndTickDetails.csb");
                break;
            }
            case 6:
            {
                std::string BtnText=pButton->getTitleText();
                TypeText->setString(BtnText);
                pTypeList->setVisible(false);
                int TypeNum=TypeConversion(BtnText);
                _selType = TypeNum;
                if (TypeNum==0) {
                    CloserMakeLayer();
                }else{
                    m_UserTopUp.clear();
                    for (auto it:(*GNS_PlayerMgr::getInstance()->getUserInfo())) {
                        if(it._type ==TypeNum){  // THIS area
                            m_UserTopUp.push_back(it);
                        }
                    }
                    AddListViewDate();
                }
                break;
            }
            case 7:
            {
                CMD_CS_SetExchangeInfo pSetInfo;
                pSetInfo._msgId=MagID;
                pSetInfo._isGet=0;//拒绝
                //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_SET_EXCHANGE_INFO, &pSetInfo, sizeof(pSetInfo));
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_SET_EXCHANGE_INFO, (char*)&pSetInfo, sizeof(pSetInfo));
                pManPopWnd->Show(false);
                vector<CMD_SC_GetUserNotExchangeInfoResult>::iterator it =GNS_PlayerMgr::getInstance()->getUserNotInfo()->begin();
                GNS_PlayerMgr::getInstance()->getUserNotInfo()->erase(it);
                RemoveItem();
                break;
            }
            case 8:
            {
                CMD_CS_SetReadFailExchangeInfo pSetReadFail;
                pSetReadFail._msgId=FaillMsgID;
                //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_HAVE_READED_FAIL_EXCHANGE_INFO, &pSetReadFail, sizeof(pSetReadFail));
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_HAVE_READED_FAIL_EXCHANGE_INFO, (char*)&pSetReadFail, sizeof(pSetReadFail));
                vector<CMD_SC_GetUserFailExchangeInfoResult>::iterator it =GNS_PlayerMgr::getInstance()->getUserFailExInfo()->begin();
                GNS_PlayerMgr::getInstance()->getUserFailExInfo()->erase(it);
                pManPopWnd->Show(false);
				RemoveItem();
                break;
            }
            default:
                break;
        }
    }
}
void GNS_UserTopUp::GetFailDate(std::string Date)
{
    pManPopWnd = PopWndLayer::createScene("Popwnd.png", "confirm_Blasting.png");
    pManPopWnd->SetTitleWithUTF("退回信息");
    pManPopWnd->SetContentTextWithGB(Date, Color3B(0, 0, 0));
    Size pot =pManPopWnd->GetPopWndSize()->getContentSize();
    pManPopWnd->GetCloseButton()->setPosition(Vec2(pot.width/2,pot.height*0.3));
    pManPopWnd->GetCloseButton()->setScale(0.5f);
    pManPopWnd->GetCloseButton()->setTag(8);
    pManPopWnd->GetCloseButton()->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::BtnCallBack, this));
}
void GNS_UserTopUp::SendDate()
{
    int Mom = GNS_PlayerMgr::getInstance()->ChooseMonNum;
    int day = GNS_PlayerMgr::getInstance()->ChooseDayNum;
    TimeText->setString(StringUtils::format("%d-%02d-%02d",year,Mom,day));
}
void GNS_UserTopUp::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        int Mon = pCalendar->Mon;
        int year = pCalendar->year;
        TimeText->setString(StringUtils::format("%d-%02d-%02d",year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        //GNS_PlayerMgr::getInstance()->getUserExInfoRes()->clear();
		GNS_PlayerMgr::getInstance()->getUserInfo()->clear();
        m_UserTopUp.clear();
        
        std::string SentDate = StringUtils::format("%d-%02d-%02d",year,Mon,atoi(BtnText.c_str()));
        CMD_CS_GetUserExchangeInfo pUserExchangelnfo;
        pUserExchangelnfo._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		memcpy(pUserExchangelnfo._selDate, SentDate.c_str(), sizeof(pUserExchangelnfo._selDate));
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_USER_EXCHANGE_INFO, (char*)&pUserExchangelnfo, sizeof(pUserExchangelnfo));
        pListView->removeAllItems();
    }
}
void GNS_UserTopUp::CloserMakeLayer()
{
    m_UserTopUp.clear();
    for (auto it:(*GNS_PlayerMgr::getInstance()->getUserInfo())) {
        m_UserTopUp.push_back(it);
    }
    
     AddListViewDate();
}
void GNS_UserTopUp::AddListViewDate()
{
    if (pListView->getItems().size()> 0 )
        pListView->removeAllItems();
//    int listViewSize = (int)m_UserTopUp.size();
    Size bgSize;
    int x=0;
    
    std::string BtnText="";
    pTypeList->setVisible(false);
    if (_selType == 0){
        BtnText = CCGnsOften::getUtf8_DirectAssignmentInProgram("总类型");
        TypeText->setString(BtnText);}
    for (auto its:(m_UserTopUp)){
            if(_selType == 0 || its._type ==_selType){  // THIS area
                auto pItemButton = Button::create("line between1.png","line between2.png");
                pItemButton->setTag(100+x);
                pItemButton->addTouchEventListener(CC_CALLBACK_2(GNS_UserTopUp::ListViewBtn, this));
                pListView->addChild(pItemButton);
                bgSize = pItemButton->getContentSize();
                
                auto pSendText = Text::create(StringUtils::format("%d",its._sendCode), GameFontName, 24);
                pSendText->setPosition(Vec2(bgSize.width*0.08, bgSize.height*0.4));
                pSendText->setColor(Color3B::BLACK);
                pItemButton->addChild(pSendText);
                
                auto pRecText = Text::create(StringUtils::format("%d",its._revCode), GameFontName, 24);
                pRecText->setPosition(Vec2(bgSize.width*0.29, bgSize.height*0.4));
                pRecText->setColor(Color3B::BLACK);
                pItemButton->addChild(pRecText);
                
                auto pGoldText = Text::create(StringUtils::format("%lld",its._gold), GameFontName, 24);
                pGoldText->setPosition(Vec2(bgSize.width*0.56, bgSize.height*0.4));
                pGoldText->setColor(Color3B::BLACK);
                pItemButton->addChild(pGoldText);
                
                std::string TypeStr = GetTypeStr(its._type);
                
                auto pType = Text::create(TypeStr, GameFontName, 24);
                pType->setPosition(Vec2(bgSize.width*0.84, bgSize.height*0.4));
                pType->setColor(Color3B::BLACK);
                pItemButton->addChild(pType);
                
                x++;
        }
        if (bgSize.width!=0||bgSize.height!=0) {
            pListView->setInnerContainerSize(Size(bgSize.width,(bgSize.height+10)*x));
        }
    }
}
void GNS_UserTopUp::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if(type==ui::Widget::TouchEventType::ENDED)
    {
        auto pButton = (Button*)pSender;
        int Btntag = pButton->getTag();
        int Index=0;
        CMD_SC_GetUserExchangeInfoResult pUserInfo;
        for (auto its:m_UserTopUp) {
            if (Index==Btntag-100) {
                pUserInfo=its;
                break;
            }
            Index++;
        }
        pGNS_UserTopUp->setVisible(false);
        pUserTopUpDea=GNS_UserTopUpDea::createLayer();
        pUserTopUpDea->SetNodeName("SysTopAndTickDetails.csb");
		pUserTopUpDea->UpPopDate(pUserInfo, GetTypeStr(pUserInfo._type));
    }

}

//void GNS_UserTopUp::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
//{
//    
//    if (type==ui::ListView::EventType::ON_SELECTED_ITEM_END)
//    {
//        auto pListView = static_cast<ListView*>(pSender);
//        CC_UNUSED_PARAM(pListView);
//        int Index = (int)pListView->getCurSelectedIndex();
//        log("%d",Index);
//    }else if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
//        auto pListView = static_cast<ListView*>(pSender);
//        CC_UNUSED_PARAM(pListView);
//        CMD_SC_GetUserExchangeInfoResult pUserInfo;
//        int Index = (int)pListView->getCurSelectedIndex();
//        int x=0;
//        for (auto its:m_UserTopUp) {
//                if (x==Index) {
//                    pUserInfo=its;
//                    break;
//                }
//            x++;
//        }
//        pGNS_UserTopUp->setVisible(false);
//        pUserTopUpDea=GNS_UserTopUpDea::createLayer();
//        pUserTopUpDea->SetNodeName("SysTopAndTickDetails.csb");
//        pUserTopUpDea->UpPopDate(pUserInfo);
//    }
//    
//}
int GNS_UserTopUp::TypeConversion(std::string type)
{
    int TypeNum = 0;
    for (int i=0; i<_DateStr.size(); i++) {
        std::string typestr =_DateStr.at(i);
        if (type == typestr) {
            TypeNum=i;
            break;
        }else{
            TypeNum=0;
        }
    }
    
    return TypeNum;
}

std::string GNS_UserTopUp::GetTypeStr(int TypeNum)
{
    std::string TypeStr="";
    for (int i=0; i<_DateStr.size(); i++) {
        if (i==TypeNum) {
            TypeStr=_DateStr.at(i);
            break;
        }
    }
    return TypeStr;
}
void GNS_UserTopUp::RemoveItem()
{
    if (GNS_PlayerMgr::getInstance()->getUserNotInfo()->size()!=0 || GNS_PlayerMgr::getInstance()->getUserFailExInfo()->size()!=0) {
        pNewManage ->setVisible(true);
    }else{
        pNewManage ->setVisible(false);
    }
}

